package com.lottery.im.module.service.ui;

import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import butterknife.BindView;
import butterknife.OnClick;
import com.fondesa.recyclerviewdivider.RecyclerViewDivider;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.integral.UserCashList;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.module.service.adapter.UserCashListAdapter;
import com.lottery.im.module.service.presenter.UserCashListPresenter;
import com.lottery.im.module.service.view.UserCashListView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import java.util.List;

/**
 * 作者：cyy
 * 时间：2018/9/20。积分明细
 */
public class UserCashListFragment extends BaseSwipeFragment<UserCashListPresenter>
    implements UserCashListView, UserCashFilterFragment.ConfirmListener {
  @BindView(R.id.recycleView) RecyclerView recycleView;
  @BindView(R.id.srf) SmartRefreshLayout srf;
  @BindView(R.id.drawerLayout) DrawerLayout drawerLayout;

  private int startRow = 0;
  private int pageSize = 10;
  private UserCashListAdapter mAdapter;

  @Override protected int getLayoutResId() {
    return R.layout.fragment_usercash_list;
  }

  @Override protected void initPresenter() {
    presenter = new UserCashListPresenter(mActivity, this, this);
    presenter.init();
  }

  @Override public void initView() {
    recycleView.setLayoutManager(new LinearLayoutManager(mActivity));
    RecyclerViewDivider.with(mActivity)
        .size((int) mActivity.getResources().getDimension(R.dimen.line_size))
        .hideLastDivider().build().addTo(recycleView);

    mAdapter = new UserCashListAdapter();
    recycleView.setAdapter(mAdapter);

    presenter.loadUserCashList(startRow, pageSize, 0);

    //下拉刷新
    srf.setOnRefreshListener(refreshLayout -> {
      startRow = 0;
      presenter.loadUserCashList(startRow, pageSize, 1);
      refreshLayout.finishRefresh();
    });

    //上拉加载更多
    srf.setOnLoadMoreListener(refreshLayout -> {
      startRow += pageSize;
      presenter.loadUserCashList(startRow, pageSize, 2);
      refreshLayout.finishLoadMore();
    });

    //筛选侧滑
    UserCashFilterFragment fragment = new UserCashFilterFragment();
    getChildFragmentManager().beginTransaction()
        .replace(R.id.fl_menu, fragment).commit();
    fragment.setOnConfrimListener(this);
    fragment.setOnResetClickListener();
  }

  @OnClick({ R.id.iv_back, R.id.tv_filter }) public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.iv_back:
        popBackStack();
        break;
      case R.id.tv_filter:
        if(drawerLayout.isDrawerOpen(Gravity.RIGHT)){
          drawerLayout.closeDrawer(Gravity.RIGHT);
        }else{
          drawerLayout.openDrawer(Gravity.RIGHT);
        }
        break;
    }
  }

  @Override public void loadUserCashListSuccess(List<UserCashList> list, int loadState) {
    if (loadState == 0 || loadState == 1) {//初次加载下拉刷新
      mAdapter.notifyList(list);
    } else {//上拉加载更多
      mAdapter.addList(list);
    }
  }

  //筛选监听
  @Override public void onConfirmListener(String startDate, String endDate, String accEn) {
    presenter.loadUserCashListFilter(startDate,endDate,accEn,0,10,0);
    drawerLayout.closeDrawer(Gravity.RIGHT);
  }

  //重置
  @Override public void resetClickListener() {
    drawerLayout.closeDrawer(Gravity.RIGHT);
    presenter.loadUserCashList(startRow=0, pageSize, 0);
  }

  @Override public void loadUserCashListFilterSuccess(List<UserCashList> list) {
    mAdapter.notifyList(list);
  }
}
